const { controller } = require('../controllers/Controller.js');

const Controllers = require('../controllers/Controller.js').controller;
const app = getApp();
function InitTime(startTime,endTime){
  var dd,hh,mm,ss = null;
  let endReplaceTime=endTime.replace(/-/g,"/");
  console.log(endReplaceTime)
  var time = (new Date(endReplaceTime).getTime() - new Date().getTime())/1000;
  if(time<=0){
      return '结束'
  }else{
      dd = Math.floor(time / 60 / 60 / 24);
      hh = Math.floor((time / 60 / 60) % 24);
      mm = Math.floor((time / 60) % 60);
      ss = Math.floor(time  % 60);
      var str = dd+"天"+hh+":"+mm+":"+ss;
      return str;
  }
}



Page({
  data: {
    shopId:'',
    marketId:'',
    marketDetail:"",
    time:""
  },
  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
  	if(app.globalData.shopId){
      this.setData({
        shopId:app.globalData.shopId,
      })
    }
    if (option.shopId) {
      this.setData({
        shopId: option.shopId
      })
      app.globalData.shopId = option.shopId
      wx.setStorageSync('shopId', option.shopId)
    }
    if(option.id){
      this.setData({
        marketId:option.id
      })
    }
    if(option.name){
      this.setData({
        name:option.name
      })
    }
    if(getCurrentPages().length<=1){
      this.setData({
        backbool:false
      })
    }else{
      this.setData({
        backbool:true
      })
    }
    if(option.scene){
      let data=decodeURIComponent(option.scene)
      console.log(data)
      let marketId=data.split(",")[0].split(":")[1]
      let shopId=data.split(",")[1].split(":")[1]
      wx.setStorageSync('shopId', shopId)
      this.setData({
        marketId:marketId
      })
      if(wx.getStorageSync('usertoken')){
        this.getMarketDetail()
      }else{
        app.usertokenReadyCallback = res => {
          this.getMarketDetail()
        }
      }
    }
  },
  onShow(){
    // if(this.data.marketDetail){

    // }else{
    //   this.getMarketDetail();
    // }
  },
  onReachBottom() {
    
  },
  // onShareAppMessage: function (e) {
      
  // },

  backindex(e){
    let page=getCurrentPages()
    let shopId=wx.getStorageSync('shopId')
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  },

  //获取拼团详情
  getMarketDetail(){
    let that=this
    let marketId=this.data.marketId
    controller.marketDetail({marketId:marketId}).then(res=>{
      this.setData({
        marketDetail:res.data,
        shopId:res.data.shopId,
        timer:setInterval(()=>{
          let time=InitTime(res.data.startTime,res.data.endTime)
          console.log(2)
          this.setData({
            time:time
          },1000)
        })
      })
      wx.setStorageSync('shopId', res.data.shopId)
      this.getShopDetail(res.data.shopId)
    })
  },

  //跳转团购专区
  toMarket(){
    wx.navigateTo({
      url: '/pages/Mall/Group?shopId='+this.data.shopId,
    })
  },
  //获取店铺详情
  getShopDetail(shopId){
    controller.shopDetails({shopId}).then(res=>{
        this.setData({
          "marketDetail.shopName":res.data.shopName
        })
    })
  },
  onUnload(){
    clearInterval(this.data.timer)
  },

  backIndex(e){
    let page=getCurrentPages()
    let shopId=this.data.shopId
    console.log(page.length)
    if(page.length<=1){
      wx.redirectTo({
        url: '/pages/Index/Index?shopId='+shopId,
      })
    }
  }
})